#include "robotcontrolwidget.h"

RobotControlWidget::RobotControlWidget(QWidget *parent) : QWidget(parent)
{
    setUI();

    //连接控制按钮组
    connect(btns_con, SIGNAL(buttonClicked(int)), this, SLOT(btns_con_clicked(int)));

    //小车控制按钮组
    connect(btns_car, SIGNAL(buttonClicked(int)), this, SLOT(btns_car_clicked(int)));

    //机械臂控制按钮组
    connect(btns_arm, SIGNAL(buttonClicked(int)), this, SLOT(btns_arm_clicked(int)));
}

void RobotControlWidget::setUI()
{
    QHBoxLayout *hbox = new QHBoxLayout;

    /* ------------------机械臂控制区 frame_func--------------------- */
    QGroupBox *frame_func = new QGroupBox;
    frame_func->setTitle("机械臂控制区");
    QVBoxLayout *vbox_func = new QVBoxLayout;
    /* 连接控制 hbox_con */
    QHBoxLayout *hbox_con = new QHBoxLayout;
    QLabel *con_lb = new QLabel("状态:");
    con_status = new QLabel("已断开");
    QPushButton *btn_connect = new QPushButton("连接");
    QPushButton *btn_disconnect = new QPushButton("断开");
    //连接按钮组
    btns_con = new QButtonGroup;
    btns_con->addButton(btn_connect, 0);
    btns_con->addButton(btn_disconnect, 1);
    //连接控制布局
    hbox_con->addWidget(con_lb);
    hbox_con->addWidget(con_status);
    hbox_con->addWidget(btn_connect);
    hbox_con->addWidget(btn_disconnect);

    /* 机械臂控制 vbox_arm */
    QVBoxLayout *vbox_arm = new QVBoxLayout;
    SliderWithBtns *controller_1 = new SliderWithBtns("舵机1");
    controller_1->setRange(0, 100);
    SliderWithBtns *controller_2 = new SliderWithBtns("舵机2");
    controller_1->setRange(0, 100);
    SliderWithBtns *controller_3 = new SliderWithBtns("舵机3");
    controller_1->setRange(0, 100);
    SliderWithBtns *controller_4 = new SliderWithBtns("夹爪");
    controller_1->setRange(0, 100);
    QHBoxLayout *hbox_jaw = new QHBoxLayout;
    QPushButton *jaw_grab = new QPushButton("抓取");
    QPushButton *jaw_release = new QPushButton("释放");
    hbox_jaw->addWidget(jaw_grab);
    hbox_jaw->addWidget(jaw_release);
    QPushButton *btn_arm_reset = new QPushButton("机械臂复位");
    btns_arm = new QButtonGroup;
    btns_arm->addButton(controller_1->btnMinus(), 0);
    btns_arm->addButton(controller_1->btnPlus(), 1);
    btns_arm->addButton(controller_2->btnMinus(), 2);
    btns_arm->addButton(controller_2->btnPlus(), 3);
    btns_arm->addButton(controller_3->btnMinus(), 4);
    btns_arm->addButton(controller_3->btnPlus(), 5);
    btns_arm->addButton(controller_4->btnMinus(), 6);
    btns_arm->addButton(controller_4->btnPlus(), 7);
    btns_arm->addButton(jaw_grab, 8);
    btns_arm->addButton(jaw_release, 9);
    btns_arm->addButton(btn_arm_reset, 10);

    vbox_arm->addWidget(controller_1);
    vbox_arm->addWidget(controller_2);
    vbox_arm->addWidget(controller_3);
    vbox_arm->addWidget(controller_4);
    vbox_arm->addLayout(hbox_jaw);

    vbox_func->addLayout(hbox_con);
    //vbox_func->addLayout(vbox_car);
    vbox_func->addLayout(vbox_arm);
    frame_func->setLayout(vbox_func);
    frame_func->setMaximumWidth(250);

    /* --------------------小车控显区 view-------------------- */
    QGroupBox *view = new QGroupBox;
    view->setTitle("小车控显区");
    QVBoxLayout *view_content = new QVBoxLayout;
    cam = new QCamera(this);
    view_finder = new QCameraViewfinder;
    cam->setViewfinder(view_finder);

    /* 小车控制 hbox_car */
    QHBoxLayout *hbox_car = new QHBoxLayout;
    QVBoxLayout *func_left = new QVBoxLayout;
    /* 巡线模式条 hbox_seek */
    QHBoxLayout *hbox_seek = new QHBoxLayout;
    QLabel *seek_text = new QLabel("巡线模式:");
    car_seekmode = new QLabel("关");
    QPushButton *btn_autoseek = new QPushButton("自动巡线");
    QPushButton *btn_stopseek = new QPushButton("停止巡线");
    hbox_seek->addWidget(seek_text);
    hbox_seek->addWidget(car_seekmode);
    hbox_seek->addWidget(btn_autoseek);
    hbox_seek->addWidget(btn_stopseek);
    /* 速度条 hbox_speed */
    QHBoxLayout *hbox_speed = new QHBoxLayout;
    QLabel *lb_speed = new QLabel("速度:");
    car_speed = new QLabel("慢");
    QPushButton *btn_speed1 = new QPushButton("慢");
    QPushButton *btn_speed2 = new QPushButton("中");
    QPushButton *btn_speed3 = new QPushButton("快");
    hbox_speed->addWidget(lb_speed);
    hbox_speed->addWidget(car_speed);
    hbox_speed->addWidget(btn_speed1);
    hbox_speed->addWidget(btn_speed2);
    hbox_speed->addWidget(btn_speed3);
    func_left->addLayout(hbox_seek);
    func_left->addLayout(hbox_speed);
    /* 小车方向键盘 vbox_direction */
    QVBoxLayout *vbox_direction = new QVBoxLayout;
    QHBoxLayout *hbox_fb = new QHBoxLayout;
    QPushButton *btn_front = new QPushButton("向前");
    QPushButton *btn_back = new QPushButton("向后");
    hbox_fb->addWidget(btn_front);
    hbox_fb->addWidget(btn_back);
    QHBoxLayout *hbox_lr = new QHBoxLayout;
    QPushButton *btn_left = new QPushButton("向左");
    QPushButton *btn_right = new QPushButton("向右");
    hbox_lr->addWidget(btn_left);
    hbox_lr->addWidget(btn_right);

    vbox_direction->addLayout(hbox_fb);
    vbox_direction->addLayout(hbox_lr);

    //小车按钮组
    btns_car = new QButtonGroup;
    btns_car->addButton(btn_front, 0);
    btns_car->addButton(btn_back, 1);
    btns_car->addButton(btn_left, 2);
    btns_car->addButton(btn_right, 3);
    btns_car->addButton(btn_speed1, 4);
    btns_car->addButton(btn_speed2, 5);
    btns_car->addButton(btn_speed3, 6);
    btns_car->addButton(btn_autoseek, 7);
    btns_car->addButton(btn_stopseek, 8);
    //小车控制布局 seek speed direction
    hbox_car->addLayout(func_left);
    hbox_car->addLayout(vbox_direction);

    view_content->addWidget(view_finder);
    view_content->addLayout(hbox_car);
    view->setLayout(view_content);
    view->setMaximumWidth(480);

    /* 日志 */
    QGroupBox *side = new QGroupBox;
    side->setTitle("行迹日志");
    QVBoxLayout *vbox_side = new QVBoxLayout;

    side->setLayout(vbox_side);

    hbox->addWidget(frame_func);
    hbox->addWidget(view);
    hbox->addWidget(side);
    this->setLayout(hbox);

}

//连接控制组
void RobotControlWidget::btns_con_clicked(int id)
{
    int flag = 0;
    switch (id) {
    //连接
    case 0:
        //开始连接 TODO
        view_finder->show();
        cam->start();

        flag = 1;
        break;
    //断开
    case 1:
        //断开连接 TODO
        cam->stop();

        flag = 2;
        break;
    default:
        break;
    }

    if(flag) con_status->setText(flag == 1 ? "已连接" : "已断开");

}

//小车控制组
void RobotControlWidget::btns_car_clicked(int id)
{
    QString cmd;
    switch (id) {
    //向前
    case 0:
        //发出指令 小车向前 TODO
        cmd = "向前";

        break;
    //向后
    case 1:
        //发出指令 小车向后 TODO
        cmd = "向后";

        break;
    //向左
    case 2:
        //发出指令 小车向左 TODO
        cmd = "向左";

        break;
    //向右
    case 3:
        //发出指令 小车向右 TODO
        cmd = "向右";

        break;
    //小车速度档1
    case 4:
        //发出指令 小车速度档1 TODO
        cmd = "小车速度档1";
        car_speed->setText("慢");

        break;
    //小车速度档2
    case 5:
        //发出指令 小车速度档2 TODO
        cmd = "小车速度档2";
        car_speed->setText("中");

        break;
    //小车速度档3
    case 6:
        //发出指令 小车速度档3 TODO
        cmd = "小车速度档3";
        car_speed->setText("快");

        break;
    //自动巡线开启
    case 7:
        //发出指令 自动巡线开启 TODO
        cmd = "自动巡线开启";
        car_seekmode->setText("开");

        break;
    //自动巡线停止
    case 8:
        //发出指令 自动巡线停止 TODO
        cmd = "自动巡线停止";
        car_seekmode->setText("关");

        break;
    default:
        break;
    }

    qDebug() << cmd;
}

void RobotControlWidget::btns_arm_clicked(int id)
{
    QString cmd;
    switch (id) {
    /* -----------舵机1------------ */
    case 0:
        //舵机1反转 TODO
        cmd = "舵机1 反转";

        break;
    case 1:
        //舵机1正转 TODO
        cmd = "舵机1 正转";

        break;
    /* ----------舵机2------------ */
    case 2:
        //舵机2反转 TODO
        cmd = "舵机2 反转";

        break;
    case 3:
        //舵机2正转 TODO
        cmd = "舵机2 正转";

        break;
    /* ----------舵机3----------- */
    case 4:
        //舵机3反转 TODO
        cmd = "舵机3 反转";

        break;
    case 5:
        //舵机3正转 TODO
        cmd = "舵机3 正转";

        break;
    /* -------夹爪角度舵机-------- */
    case 6:
        //夹爪角度 正转 TODO
        cmd = "夹爪 反转";

        break;
    case 7:
        //夹爪角度 反转 TODO
        cmd = "夹爪 正转";

        break;
    /* ------夹爪动作控制--------- */
    case 8:
        //夹爪 抓取 TODO
        cmd = "夹爪 抓取";

        break;
    case 9:
        //夹爪 释放 TODO
        cmd = "夹爪 释放";

        break;
    /* --------机械臂复位--------- */
    case 10:
        //机械臂 复位 TODO
        cmd = "机械臂 复位";

        break;
    default:
        break;
    }
    qDebug() << cmd;
}
